/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.madding.shared.encrypt.cert.bc.loader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyPair;
import java.security.Security;
import java.security.cert.Certificate;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMReader;

import com.madding.shared.lang.ClassLoaderUtil;

/**
 * 类CertCaUtil.java的实现描述：ca文件到内存持久化工具类
 * 
 * @author madding.lip Dec 6, 2013 9:30:09 PM
 */
public class CaCertLoader {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private static final String CA_CRT_FILE        = "META-INF/bc_certca/alilang_ca.crt";
    private static final String CA_KEY_FILE        = "META-INF/bc_certca/alilang_ca.key";
    private static final String CLIENT_CA_CRT_FILE = "META-INF/bc_certca/alilang_client_ca.crt";
    private static final String CLIENT_CA_KEY_FILE = "META-INF/bc_certca/alilang_client_ca.key";

    private static Certificate  caCrt;
    private static KeyPair      caKeyPair;
    private static Certificate  clientCaCrt;
    private static KeyPair      clientCaKeyPair;

    public synchronized static Certificate getCaCrt() throws IOException {
        if (caCrt == null) {
            ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
            InputStream is = classLoader.getResourceAsStream(CA_CRT_FILE);
            PEMReader reader = new PEMReader(new InputStreamReader(is));
            caCrt = (Certificate) reader.readObject();
            reader.close();
            is.close();
        }
        return caCrt;
    }

    public synchronized static KeyPair getCaKeyPair() throws IOException {
        if (caKeyPair == null) {
            ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
            InputStream is = classLoader.getResourceAsStream(CA_KEY_FILE);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            PEMReader reader = new PEMReader(in);
            caKeyPair = (KeyPair) reader.readObject();
            reader.close();
            is.close();
        }
        return caKeyPair;
    }

    public synchronized static Certificate getClientCaCrt() throws IOException {
        if (clientCaCrt == null) {
            ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
            InputStream is = classLoader.getResourceAsStream(CLIENT_CA_CRT_FILE);
            PEMReader reader = new PEMReader(new InputStreamReader(is));
            clientCaCrt = (Certificate) reader.readObject();
            reader.close();
            is.close();
        }
        return clientCaCrt;
    }

    public synchronized static KeyPair getClientCaKeyPair() throws IOException {
        if (clientCaKeyPair == null) {
            ClassLoader classLoader = ClassLoaderUtil.getClassLoader();
            InputStream is = classLoader.getResourceAsStream(CLIENT_CA_KEY_FILE);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            PEMReader reader = new PEMReader(in);
            clientCaKeyPair = (KeyPair) reader.readObject();
            reader.close();
            is.close();
        }
        return clientCaKeyPair;
    }

}
